//
//  ZYStudentListController.m
//  优学荟管理系统
//
//  Created by Ethan on 2017/9/19.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYStudentListController.h"
#import "ZYStudentLstCell.h"
#import "ZYStudentListModel.h"
#define kCell @"cell"
#import "HNStudentDetailView.h"
@interface ZYStudentListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *allView;
@property (nonatomic,strong) UILabel *rmbLabel;//金额
@property (nonatomic,strong) UILabel *sumLabel;//缴费
@property (nonatomic,strong) UILabel *studentLabel;//学生
@property (nonatomic,strong) UILabel *studentSunLabel;//学生人数
@property (nonatomic,strong) UILabel *accountNameLabel;//账户名
@property (nonatomic,strong) UILabel *userNameLabel;//用户名
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;

@end

@implementation ZYStudentListController


- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"学生列表";
    
   
    [self setUpSuperView];
    
    [self setUpRefresh];
    
}

- (void)setUpSuperView{
    
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 140  + 60 + 15)];
  
    
    UIImageView *photoIamgeView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 140)];
    
//    photoIamgeView.image = [UIImage imageNamed:@"list_bg_default"];
    photoIamgeView.backgroundColor = ColorRGB(42, 197, 127);
    [self.allView addSubview:photoIamgeView];
    
    
    self.rmbLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,60, SCREEN_WIDTH/2 - 20, 20)];
    self.rmbLabel.textColor = ColorRGB(254, 254, 254);
    self.rmbLabel.font = [UIFont systemFontOfSize:20];
    [photoIamgeView addSubview:self.rmbLabel];
    
    
    
    
    self.sumLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.rmbLabel.frame.origin.x , self.rmbLabel.frame.origin.y + self.rmbLabel.frame.size.height + 15, self.rmbLabel.frame.size.width, self.rmbLabel.frame.size.height)];
    self.sumLabel.font = [UIFont systemFontOfSize:14];
    
    self.sumLabel.textColor = ColorRGB(254,254,254);
    self.sumLabel.text = @"当月共缴费(元)";
    [photoIamgeView addSubview:self.sumLabel];
    
    
    self.studentLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.rmbLabel.frame.origin.x + self.rmbLabel.frame.size.width + 30, self.rmbLabel.frame.origin.y  , self.rmbLabel.frame.size.width, self.rmbLabel.frame.size.height)];

    self.studentLabel.font  = [UIFont systemFontOfSize:20];
    self.studentLabel.textColor = ColorRGB(254, 254,254);
    [photoIamgeView addSubview:self.studentLabel];
    
    
    
    self.studentSunLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.sumLabel.frame.origin.x + self.sumLabel.frame.size.width + 30, self.sumLabel.frame.origin.y, self.sumLabel.frame.size.width, self.sumLabel.frame.size.height)];
    
    self.studentSunLabel.textColor = ColorRGB(254, 254, 254);
    self.studentSunLabel.text = @"学生人数(人)";
    self.studentSunLabel.font = [UIFont systemFontOfSize:14];
  
    [photoIamgeView addSubview:self.studentSunLabel];
    
    
    
    self.accountNameLabel  = [[UILabel alloc] initWithFrame:CGRectMake(0, photoIamgeView.frame.origin.y + photoIamgeView.frame.size.height, SCREEN_WIDTH/3, 60)];
    
    self.accountNameLabel.font = [UIFont systemFontOfSize:14];
    self.accountNameLabel.textColor = ColorRGB(153, 153, 153);
    self.accountNameLabel.text = @"账户名";
    self.accountNameLabel.textAlignment = NSTextAlignmentCenter;
    [self.allView addSubview:self.accountNameLabel];
    
    
    

    self.userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.accountNameLabel.frame.origin.x + self.accountNameLabel.frame.size.width,self.accountNameLabel.frame.origin.y, self.accountNameLabel.frame.size.width, self.accountNameLabel.frame.size.height)];
    
    self.userNameLabel.font = [UIFont systemFontOfSize:14];
    self.userNameLabel.textColor = ColorRGB(153, 153, 153);
    self.userNameLabel.text = @"用户名";
    self.userNameLabel.textAlignment = NSTextAlignmentCenter;
    [self.allView addSubview:self.userNameLabel];
    
    
    UILabel  *grayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.accountNameLabel.frame.origin.y + self.accountNameLabel.frame.size.height, SCREEN_WIDTH,15)];
    grayLabel.backgroundColor = ColorRGB(247, 244, 248);
    [self.allView addSubview:grayLabel];
    
    
    
    
    
    
    
    
    
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, SCREENH_HEIGHT  - 64)];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ZYStudentLstCell  class] forCellReuseIdentifier:kCell];
    self.tableView.tableHeaderView = self.allView;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.estimatedRowHeight = 200;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);


    
    
    
    
}


#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}



- (void)setNetWork{
    
 
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=studentListByTeacherUid&uid=%@&pageNumber=%ld",self.uid,(long)self.page];
    
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool  shareAccountTool].currentAccount.authCode);
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];

    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"]  integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            
            NSDictionary *dict = [Obj valueForKey:@"t"];
            
            NSArray *arr = [dict  valueForKey:@"dataList"];
            
            self.rmbLabel.text = [NSString stringWithFormat:@"%.2f",[[dict valueForKey:@"withdrawMoney"] doubleValue]];
            self.studentLabel.text = [NSString stringWithFormat:@"%@",[dict valueForKey:@"customParam"]];
            
            for (NSDictionary *dic  in arr) {
                
                ZYStudentListModel *model = [ZYStudentListModel new];
                
                [model setValuesForKeysWithDictionary:dic];
                
                
                [self.dataSource addObject:model];
                
                
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
            
            
        }
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYStudentLstCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    if (self.dataSource.count > indexPath.row) {
        ZYStudentListModel *model = self.dataSource[indexPath.row];
        
        
        [cell assignByZYStudentListModel:model];
        
        
    }
    
    [cell.studentButton addTarget:self action:@selector(studentButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    return cell;
}


#pragma mark ---- 详情方法的实现
- (void)studentButtonAction:(UIButton *)sender{
    
    ZYStudentLstCell *cell = (ZYStudentLstCell *)sender.superview.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYStudentListModel *model = self.dataSource[indexPath.row];

    
    [HNStudentDetailView ShowWithName:model.petname grade:[NSString stringWithFormat:@"%@",model.grade] className:model.lessonName count:[NSString stringWithFormat:@"%@/%@",model.recordCount,model.courseCount] complete:^{
        // 消失后的事件
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];
    
    
}

@end
